//
//  JYJVCOfSechContent.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/8.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfSechContent.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJSeachContentTitleCollectionViewCell.h"
#import "JYJSechContentSegmentCollectionViewCell.h"
#import "JYJSeachContentProgramCollectionViewCell.h"
#import "Masonry.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"

@interface JYJVCOfSechContent ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *titleCollection;
@property (nonatomic, strong) UIView *colorView;

@property (nonatomic, strong) BaseCollectionView *bigCollection;
@property (nonatomic, assign) NSInteger labelIndex;
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
@end

@implementation JYJVCOfSechContent

- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    self.view.backgroundColor = [UIColor colorWithRed:0.41 green:0.42 blue:0.47 alpha:1.00];
    [self createTitleCollection];
    [self createView];
    
    [self createBigCollection];
    [self createKVOAction];
    // Do any additional setup after loading the view.
}
- (void)createTitleCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 3, 40);
    layout.minimumLineSpacing = 0;
    self.titleCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) collectionViewLayout:layout];
    [self.view addSubview:_titleCollection];
    self.titleCollection.delegate = self;
    self.titleCollection.dataSource = self;
    [self.titleCollection registerClass:[JYJSeachContentTitleCollectionViewCell class] forCellWithReuseIdentifier:@"Title"];
    self.titleCollection.showsHorizontalScrollIndicator = NO;
}
- (void)createBigCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.bigCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44) collectionViewLayout:layout];
    [self.view addSubview:_bigCollection];
    self.bigCollection.delegate = self;
    self.bigCollection.dataSource = self;
    [self.bigCollection registerClass:[JYJSechContentSegmentCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    self.bigCollection.pagingEnabled = YES;
    self.bigCollection.showsHorizontalScrollIndicator = NO;
    [self.bigCollection registerClass:[JYJSeachContentProgramCollectionViewCell class] forCellWithReuseIdentifier:@"program"];
    [self.bigCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Anchor"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _titleCollection) {
        JYJSeachContentTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Title" forIndexPath:indexPath];
        NSArray *arr = @[@"栏目",@"节目",@"主播"];
        cell.titleLabel.text = arr[indexPath.item];
        if (indexPath.item == _labelIndex) {
            cell.titleLabel.textColor = [UIColor whiteColor];
        }else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        if (indexPath.item == _oldIndexPath.item) {
            cell.titleLabel.textColor = [UIColor whiteColor];
        }else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        return cell;
        
    }else {
        if (indexPath.item == 0) {
            JYJSechContentSegmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
            __weak typeof(self) weakSelf = self;
            cell.handleNext = ^(JYJVCOfFeaturedTopThreeDetails *details) {
                weakSelf.tabBarController.tabBar.hidden = YES;
                weakSelf.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:details animated:YES];
                weakSelf.hidesBottomBarWhenPushed = NO;
            };
            cell.inputWord = self.inPutWord;
            return cell;
            
        }else if (indexPath.item == 1){
            JYJSeachContentProgramCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"program" forIndexPath:indexPath];
            __weak typeof(self) weakSelf = self;
            cell.handleAction = ^(JYJVCOfFeaturedTopThreeDetails *details) {
                weakSelf.tabBarController.tabBar.hidden = YES;
                weakSelf.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:details animated:YES];
                weakSelf.hidesBottomBarWhenPushed = NO;
            };

            cell.inPutWord = self.inPutWord;
            return cell;
        }else {
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Anchor" forIndexPath:indexPath];
            UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download_nologin"]];
            image.frame = CGRectMake(0, 0, 150,150);
            image.center = cell.contentView.center;
            [cell.contentView addSubview:image];
            UILabel *label = [[UILabel alloc]init];
            label.text = @"未搜到相关内容";
            label.textAlignment = 1;
            label.font = [UIFont systemFontOfSize:25];
            [cell.contentView addSubview:label];
            label.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.51 alpha:1.00];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView.mas_left).offset(100);
                make.right.equalTo(cell.contentView.mas_right).offset(-100);
                make.bottom.equalTo(image.mas_top).with.offset(20);
                make.height.mas_equalTo(80);
            }];
            

            return cell;
        }
    }
}
- (void)createView {
    self.colorView = [[UIView alloc]initWithFrame:CGRectMake(0,40, SCREEN_WIDTH / 3, 3)];
    _colorView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_colorView];
    
}
// 添加观察者
- (void)createKVOAction {
    [self.bigCollection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat X = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = X / 3;
    self.colorView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bigCollection) {
        //当前大Collection的第几个item
        NSInteger currentItem = scrollView.contentOffset.x / SCREEN_WIDTH;
        // 根据第几个item获取当前的indexPath
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        // 根据indexpaht获取当前的标题的Collection
        JYJSeachContentTitleCollectionViewCell *cell = ( JYJSeachContentTitleCollectionViewCell *)[self.titleCollection cellForItemAtIndexPath:currentIndexPath];
        //字体变色
        cell.titleLabel.textColor = [UIColor whiteColor];
        // 恢复字体颜色
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        //加判防止滑一点点就就开，并没有划入新页面，字体就变色了
        if (currentIndexPath != indexPathPast) {
             JYJSeachContentTitleCollectionViewCell *cell = ( JYJSeachContentTitleCollectionViewCell *)[self.titleCollection cellForItemAtIndexPath:indexPathPast];
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        //记录当前的item 和 划过的indexPath
        self.labelIndex = currentItem;
        self.oldIndexPath = currentIndexPath;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.titleCollection) {
        [self.bigCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
         JYJSeachContentTitleCollectionViewCell *cell = ( JYJSeachContentTitleCollectionViewCell *)[self.titleCollection cellForItemAtIndexPath:indexPath];
        cell.titleLabel.textColor = [UIColor whiteColor];
         JYJSeachContentTitleCollectionViewCell *pastCell = ( JYJSeachContentTitleCollectionViewCell *)[self.titleCollection cellForItemAtIndexPath:_oldIndexPath];
        if (indexPath != _oldIndexPath) {
            pastCell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        self.oldIndexPath = indexPath;
        self.labelIndex = _oldIndexPath.item;
    }
}
- (void)dealloc {
    [self.bigCollection removeObserver:self forKeyPath:@"contentOffset"];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
